package com.xc.question.web;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mysql.cj.log.Log;
import com.xc.question.entity.*;
import com.xc.question.service.*;
import com.yjf.question.utils.ResultUtils;
import com.yjf.question.utils.ResultVo;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Author: 星川
 * @Date: 2023/04/12/16:22
 * @Description:
 */
@RestController
@RequestMapping("/wxapi/home")
public class HomeController {
    @Resource
    private SysQuestionService sysQuestionService;

    @Resource
    private AnswerQuestionService answerQuestionService;

    @Resource
    private SysPaperService sysPaperService;

    @Resource
    private SysPaperChoiceService sysPaperChoiceService;




    /**
     * 查询
     * @param sysQuestionParn
     * @return
     */
    @RequestMapping("/getList")
    public ResultVo getList(SysQuestionParn sysQuestionParn){
        Page<SysQuestion> page = new Page<>();
        page.setCurrent(sysQuestionParn.getCurrentPage());
        page.setSize(sysQuestionParn.getPageSize());
        IPage<SysQuestion> list = sysQuestionService.getList(page);
        return ResultUtils.success("查询成功",list);
    }

    /**
     *
     * @param openid
     * @param questionId
     * @return 根据问卷id查询2、根据问卷id和openid查询
     */
    @RequestMapping("/getDetails")
    public ResultVo gerDetails(String openid , String questionId){
        ///获取id
        SysQuestion serviceById = sysQuestionService.getById(questionId);
        //构造条件构造器
        QueryWrapper<AnswerQuestion> wrapper = new QueryWrapper<>();
        wrapper.lambda().eq(AnswerQuestion::getQuestionId,questionId)
                .eq(AnswerQuestion::getOpenid,openid);
        AnswerQuestion question = answerQuestionService.getOne(wrapper);
        if (question != null){
            serviceById.setStatus("1");
        }else {
            serviceById.setStatus("0");
        }
        return ResultUtils.success("查询成功",serviceById);
    }


    /**
     * 根据问卷id查询试题
     * @param questionId
     * @return
     */
    @GetMapping("/getPaperList")
    public ResultVo getPaperList(String questionId){
        // 1、查询问卷
        SysQuestion question = sysQuestionService.getById(questionId);
        //2、根据问卷id查试题列表
        //构造查询条件
        QueryWrapper<SysPaper> query = new QueryWrapper<>();
        query.lambda().eq(SysPaper::getQuestionId,questionId);
        //试题列表
        List<SysPaper> paperList = sysPaperService.list(query);
        // 3、查询单选和多选对应的选项
        for (int i=0;i<paperList.size();i++){
            //如果是单选或者多选的时候，需要查询选项
            if(paperList.get(i).getPaperType().equals("1") || paperList.get(i).getPaperType().equals("2")){
                QueryWrapper<SysPaperChoice> queryWrapper = new QueryWrapper<>();
                //构造多选表试题的关系
                queryWrapper.lambda().eq(SysPaperChoice::getPaperId,paperList.get(i).getPaperId());
                //查询所有的选择题内容
                List<SysPaperChoice> paperChoices = sysPaperChoiceService.list(queryWrapper);
                //设置单选和多选的选项
                paperList.get(i).setPaperChoice(paperChoices);
            }
        }
        //给问卷设置试题
        question.setListPaper(paperList);
        return ResultUtils.success("查询成功",question);
    }


    /**
     * 保存问卷
     * @param commitParm
     * @return
     */
    @PostMapping("/saveCommit")
    public ResultVo saveCommit(@RequestBody CommitParm commitParm){
        answerQuestionService.saveCommit(commitParm);
        return ResultUtils.success("提交成功");
    }

    /**
     * 获取我的问卷列表
     * @param sysQuestionParn
     * @return
     */
    @RequestMapping("/getMyQuestionList")
    public ResultVo getMyQuestionList(SysQuestionParn sysQuestionParn){
        //构造条件
        Page<SysQuestion> page = new Page<>();
        page.setCurrent(sysQuestionParn.getCurrentPage());
        page.setSize(sysQuestionParn.getPageSize());
        IPage<SysQuestion> myQuestionList = sysQuestionService.getMyQuestionList(page, sysQuestionParn.getOpenid());
        return ResultUtils.success("查询成功",myQuestionList);
    }


    /**
     *
     * @param questionId
     * @param openId
     * @return查询试题回显
     */
    @RequestMapping("/getMyPaperListShow")
    public ResultVo getMyPaperListShow(Long questionId , String openId){
        SysQuestion paperList = sysQuestionService.getMyPaperList(questionId, openId);
        return ResultUtils.success("查询成功",paperList);
    }



}
